package org.lex.dbq.view.template;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.lex.dbq.model.bo.JdbcUrlTemplate;
import org.lex.dbq.model.bo.Variable;

public class VariableTable extends Composite {
	private TableViewer viewer = null;

	public VariableTable(Composite parent) {
		super(parent, SWT.NONE);
		initialize();
	}

	public void setInput(JdbcUrlTemplate url) {
		viewer.setInput(url.getVariables());
	}

	protected void initialize() {
		setLayout(new FillLayout());
		Table table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);

		viewer = buildAndLayoutTable(table);
		attachContentProvider(viewer);
		attachLabelProvider(viewer);
		attachCellEditors(viewer, table);
	}

	private void attachLabelProvider(TableViewer viewer) {
		viewer.setLabelProvider(new ITableLabelProvider() {
			public Image getColumnImage(Object element, int columnIndex) {
				return null;
			}

			public String getColumnText(Object element, int columnIndex) {
				return VariableMetaData.getProperty((Variable) element, columnIndex).toString();
			}

			public void addListener(ILabelProviderListener listener) {
			}

			public void dispose() {
			}

			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			public void removeListener(ILabelProviderListener lpl) {
			}
		});
	}

	private void attachContentProvider(TableViewer viewer) {
		viewer.setContentProvider(new ArrayContentProvider());
	}

	private TableViewer buildAndLayoutTable(final Table table) {
		for (String name : VariableMetaData.PROPERTY_DISPLAY_NAMES) {
			TableColumn column = new TableColumn(table, SWT.LEFT);
			column.setText(name);
			column.pack();
		}
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		return new TableViewer(table);
	}

	private void attachCellEditors(final TableViewer viewer, Composite parent) {
		viewer.setCellModifier(new ICellModifier() {
			public boolean canModify(Object element, String property) {
				return true;
			}

			public Object getValue(Object element, String property) {
				return VariableMetaData.getProperty((Variable) element, property);
			}

			public void modify(Object element, String property, Object value) {
				TableItem tableItem = (TableItem) element;
				Variable var = (Variable) tableItem.getData();
				VariableMetaData.setProperty(var, property, value);
				viewer.refresh(var);
			}
		});
		viewer.setCellEditors(new CellEditor[] {//
		new TextCellEditor(parent, SWT.LEFT),// name
				new TextCellEditor(parent, SWT.CENTER),// display name
				new TextCellEditor(parent, SWT.CENTER),// default value
				new CheckboxCellEditor(parent, SWT.CENTER), // required
				new CheckboxCellEditor(parent, SWT.CENTER),// secert
				new TextCellEditor(parent, SWT.CENTER) // description
		});
		viewer.setColumnProperties(VariableMetaData.PROPERTY_NAMES);
	}
}
